<?php

use plugin\cms\app\bll\Category as BllCategory;
use plugin\cms\app\bll\Rewrite;
use Shopwwi\LaravelCache\Cache;

/**
 * 当前伪静态
 *
 * @return array
 */
function cms_rewite(): array
{
    $nowrewrite = Cache::get('cms_now_rewrite');
    if ($nowrewrite) {
        return $nowrewrite;
    }
    $bllRewrite = new Rewrite();
    $rewrite = $bllRewrite->findByUse();
    Cache::add('cms_now_rewrite', $rewrite);
    return $rewrite;
}


/**
 * 生成列表URL地址
 *
 * @param integer $cid
 * @return string
 */
function cms_cate_url(int $cid): string
{
    $nowrewrite = cms_rewite();
    if ($nowrewrite) {
        return str_replace('{cid}', $cid, $nowrewrite['cate']);
    }
    return '/app/cms/home/Index/category?cid=' . $cid;
}

/**
 * 栏目分页URL
 *
 * @param integer $cid
 * @param integer $page
 * @param boolean $pagetionuse  是否供分页使用
 * @return string
 */
function cms_cate_page_url(
    int $cid,
    bool $pagetionuse = true,
    ?int $page = null

): string {
    $nowrewrite = cms_rewite();
    if ($nowrewrite) {
        $url = str_replace('{cid}', $cid, $nowrewrite['catepage']);

        if ($pagetionuse) {
            $url = str_replace('{page}', '(:num)', $url);
        } else {
            $url = str_replace('{page}', $page, $url);
        }

        return $url;
    }
    if ($pagetionuse) {
        return '/app/cms/home/Index/category?cid=' . $cid . '&page=(:num)';
    }
    return '/app/cms/home/Index/category?cid=' . $cid . '&page=' . $page;
}

/**
 * 获取文章链接
 *
 * @param array $content
 * @return string
 */
function cms_content_url(array|object $content): string
{
    if (is_object($content)) {
        $content = (array) $content;
    }

    $nowrewrite = cms_rewite();
    if ($nowrewrite) {
        return str_replace(['{cid}', '{id}'], [$content['cid'], $content['id']], $nowrewrite['page']);
    }
    return '/app/cms/home/Index/page?cid=' . $content['cid'] . '&id=' . $content['id'];
}


/**
 * 生成首页地址链接
 *
 * @param array|object $content
 * @return string
 */
function cms_index_url(): string
{

    $nowrewrite = cms_rewite();
    if ($nowrewrite) {
        return $nowrewrite['index'];
    }
    return '/app/cms/home/Index/index';
}

/**
 * 生成搜索页链接
 *
 * @param array $content
 * @return string
 */
function cms_search_url(): string
{

    $nowrewrite = cms_rewite();
    if ($nowrewrite) {
        return $nowrewrite['search'];
    }
    return '/app/cms/home/Index/search';
}

/**
 * 面包屑
 *
 * @param integer $cid
 * @param integer $siteid
 * @return array
 */
function cms_crumb(?int $cid = null, ?int $siteid = null): array
{
    $cid = $cid == null ? request()->get('cid') : $cid;
    $siteid = $siteid == null ? now_site()['id'] : $cid;

    $bllCategory = new BllCategory();
    return $bllCategory->getAncestorsAndSelf($cid, $siteid);
}

function cms_crumb_quick(
    ?int $cid = null,
    ?int $siteid = null,
    string $separate = ' / ',
    bool $containhome = true,
    string $target = '_blank'
) {

    $cid = $cid == null ? request()->get('cid') : $cid;
    $siteid = $siteid == null ? now_site()['id'] : $cid;

    $bllCategory = new BllCategory();
    $cates = $bllCategory->getAncestorsAndSelf($cid, $siteid);
    $curmb = $containhome ? '<a href="/">首页</a>' . $separate : '';
    foreach ($cates as $cate) {
        $curmb .= '<a target="' . $target . '" href="' . cms_cate_url($cate['id']) . '">' . $cate['title'] . '</a>' . $separate;
    }
    return $curmb;
}
